<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="督导姓名" prop="supName">
        <el-input
          v-model="queryParams.supName"
          placeholder="请输入督导姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任课教师" prop="tName">
        <el-input
          v-model="queryParams.tName"
          placeholder="请输入任课教师"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['course:attend:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['course:attend:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['course:attend:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['course:attend:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="attendList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="100" align="center">
        <template slot-scope="scope">
          {{scope.$index + 1}}
        </template>
      </el-table-column>
      <el-table-column label="听课类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.supms_course_type"
            :value="scope.row.type"
          />
        </template>
      </el-table-column>
      <el-table-column label="督导姓名" align="center" prop="supName" />
      <el-table-column label="课程名称" align="center" prop="courseName" />
      <el-table-column label="任课教师" align="center" prop="tName" />
      <el-table-column label="授课地点" align="center" prop="teachLocal" />
      <el-table-column label="听课评价" width="150" align="center" prop="attendEvalFlag">
        <template slot-scope="scope">
          <div v-if="scope.row.attendEvalFlag === '0'">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-circle-plus-outline"
              @click="toAttendEval(scope.row)"
              >去评价</el-button
            >
          </div>
          <div v-else-if="scope.row.attendEvalFlag === '1'">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="attendEvaSee(scope.row)"
              >查看听课评价</el-button
            >
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="openAttendTable(scope.row)"
            >查看听课表</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['course:attend:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['course:attend:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改课堂听课对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-row :gutter="15">
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-col :span="12">
            <el-form-item label="听课类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择听课类型">
                <el-option
                  v-for="dict in dict.type.supms_course_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="课程名称" prop="courseName">
              <el-input
                v-model="form.courseName"
                placeholder="请输入课程名称"
                :maxlength="11"
                clearable
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任课教师" prop="tName">
              <el-input
                v-model="form.tName"
                placeholder="请输入任课教师"
                :maxlength="11"
                clearable
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="教师工号" prop="tNum">
              <span slot="label">
                <el-tooltip content="可在职工管理中查询，建议先填写其他重要信息保存后再补充">
                <i class="el-icon-info"></i>
                </el-tooltip>
                教师工号
              </span>
              <el-input
                v-model="form.tNum"
                placeholder="建议输入教师工号"
                oninput ="value=value.replace(/[^0-9.]/g,'')"
                :maxlength="11"
                clearable
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属部门" prop="tDep">
              <span slot="label">
                <el-tooltip content="可在职工管理中查询，建议先填写其他重要信息保存后再补充">
                <i class="el-icon-info"></i>
                </el-tooltip>
                所属部门
              </span>
              <el-input
                v-model="form.tDep"
                placeholder="建议输入教师的所属部门"
                clearable
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="授课班级" prop="teachClass">
              <el-input
                v-model="form.teachClass"
                placeholder="请输入授课班级"
                clearable
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="教学章节" prop="teachSection">
              <el-input
                v-model="form.teachSection"
                placeholder="请输入教学章节"
                clearable
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="授课地点" prop="teachLocal">
              <el-input
                v-model="form.teachLocal"
                placeholder="请输入授课地点"
                clearable
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="学生出勤" prop="stuAtte">
              <el-input
                v-model="form.stuAtte"
                placeholder="请输入学生出勤情况（应到、实到、迟到、早退）"
                clearable
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="授课时间" prop="teachTime">
              <span slot="label">
                <el-tooltip content="请按格式输入：xxxx年xx月xx日，第x周星期x第xx节">
                <i class="el-icon-question"></i>
                </el-tooltip>
                授课时间
              </span>
              <el-input
                v-model="form.teachTime"
                placeholder="格式：xxxx年xx月xx日，第x周星期x第xx节"
                clearable
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="教学/实验过程记录" prop="process">
              <el-input
                v-model="form.process"
                type="textarea"
                placeholder="请输入教学/实验过程记录"
                :maxlength="200"
                show-word-limit
                :autosize="{ minRows: 4, maxRows: 4 }"
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="值得肯定、学习、借鉴或推广" prop="alrp">
              <el-input
                v-model="form.alrp"
                type="textarea"
                placeholder="值得肯定、学习、借鉴或推广的是："
                :maxlength="200"
                show-word-limit
                :autosize="{ minRows: 4, maxRows: 4 }"
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="值得商榷或改进" prop="discussingOrImproving">
              <el-input
                v-model="form.discussingOrImproving"
                type="textarea"
                placeholder="值得商榷或改进的是："
                :maxlength="200"
                show-word-limit
                :autosize="{ minRows: 4, maxRows: 4 }"
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 查看听课表 -->
    <el-dialog :title="title" :visible.sync="openTable" width="800px" append-to-body>
      <el-descriptions class="margin-top" :model="form" title="重庆三峡学院听课卡" :column="2" border>

        <template slot="extra">
          {{"填表日期：" + parseTime(form.createTime, '{y}-{m}-{d}')}}
        </template>

        <el-descriptions-item>
          <template slot="label">
            课程类型
          </template>
          <dict-tag :options="dict.type.supms_course_type" :value="form.type"/>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            听课人
          </template>
          {{form.supName}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            任课教师
          </template>
          {{form.tName}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            所在学院
          </template>
          {{form.tDep}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            课程名称
          </template>
          {{form.courseName}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            教学章节
          </template>
          {{form.teachSection}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            授课班级
          </template>
          {{form.teachClass}}
        </el-descriptions-item>
         <el-descriptions-item>
          <template slot="label">
            学生出勤
          </template>
          {{form.stuAtte}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            授课时间
          </template>
          {{form.teachTime}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            授课地点
          </template>
          {{form.teachLocal}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            评价总分
          </template>
          {{form.totalScore}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            评价等级
          </template>
          {{form.level}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            教学(或实验)过程记录
          </template>
          {{form.process}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            只得肯定、学习、借鉴、或推广的是
          </template>
          {{form.alrp}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            值得商榷或改进的是
          </template>
          {{form.discussingOrImproving}}
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAttend,
  getAttend,
  delAttend,
  addAttend,
  updateAttend,
} from "@/api/course/attend";

export default {
  name: "Attend",
  dicts: ["supms_course_type", "supms_attend_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课堂听课表格数据
      attendList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 显示查看听课表模态框
      openTable: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supName: null,
        tName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        type: [
          {
            required: true,
            message: "请选择听课类型",
            trigger: "change",
          },
        ],
        tName: [
          {
            required: true,
            message: "请输入任课教师",
            trigger: "blur",
          },
        ],
        courseName: [
          {
            required: true,
            message: "请输入课程名称",
            trigger: "blur",
          },
        ],
        teachClass: [
          {
            required: true,
            message: "请输入授课班级",
            trigger: "blur",
          },
        ],
        teachSection: [
          {
            required: true,
            message: "请输入教学章节",
            trigger: "blur",
          },
        ],
        teachLocal: [
          {
            required: true,
            message: "请输入授课地点",
            trigger: "blur",
          },
        ],
        stuAtte: [
          {
            required: true,
            message: "请输入学生的出勤情况",
            trigger: "blur",
          },
        ],
        teachTime: [
          {
            required: true,
            message: "请输入本次授课的时间",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询课堂听课列表 */
    getList() {
      this.loading = true;
      listAttend(this.queryParams).then((response) => {
        // console.log(response.rows);
        this.attendList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        supName: null,
        tName: null,
        tNum: null,
        tDep: null,
        teachClass: null,
        stuAtte: null,
        courseName: null,
        teachSection: null,
        teachTime: null,
        teachLocal: null,
        type: null,
        attendEvalFlag: null,
        totalScore: null,
        level: null,
        process: null,
        alrp: null,
        discussingOrImproving: null,
        delFlag: null,
        createTime: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加课堂听课";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getAttend(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改课堂听课";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateAttend(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAttend(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除该课堂听课数据？')
        .then(function () {
          return delAttend(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "course/attend/export",
        {
          ...this.queryParams,
        },
        `attend_${new Date().getTime()}.xlsx`
      );
    },
    /** 去评价按钮操作 */
    toAttendEval(row) {
      const attendId = row.id
      const typeNum = row.type
      this.$router.push({ path: '/course/attendEval/toAE', query: { attendId: attendId, typeNum: typeNum, pageNum: this.queryParams.pageNum } });
    },
    /** 查看评价按钮操作 */
    attendEvaSee(row) {
      const attendId = row.id
      this.$router.push({ path: '/course/attendEval/getAE', query: { attendId: attendId, pageNum: this.queryParams.pageNum } });
    },
    /** 查看听课表按钮操作 */
    openAttendTable(row) {
      this.reset();
      const id = row.id
      getAttend(id).then((res) => {
        this.form = res.data;
        this.openTable = true;
        this.title = "查看听课表";
      })
    }
  },
};
</script>
